/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictBizCache;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.dto.BuildingSelectSearchDTO;
import org.springblade.modules.repair.entity.Building;
import org.springblade.modules.repair.entity.BuildingContacts;
import org.springblade.modules.repair.entity.BuildingExt;
import org.springblade.modules.repair.service.BuildingContactsService;
import org.springblade.modules.repair.service.ElevatorBrandService;
import org.springblade.modules.repair.service.IBuildingExtService;
import org.springblade.modules.repair.service.IBuildingService;
import org.springblade.modules.repair.vo.*;
import org.springblade.modules.repair.vo.statistics.ChartItemVO;
import org.springblade.modules.repair.vo.statistics.PieChartVO;
import org.springblade.modules.repair.wrapper.BuildingWrapper;
import org.springblade.modules.system.entity.DictBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼盘信息 控制器
 *
 * @author BladeX
 * @since 2021-05-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/building")
@Api(value = "楼盘信息", tags = "楼盘信息管理")
public class BuildingController extends BladeController {

	private static final String PROJECT_TYPE_CODE = "building_type";
	private final IBuildingService buildingService;
	private final ElevatorBrandService elevatorBrandService;
	private final BuildingContactsService buildingContactsService;
	private final IBuildingExtService buildingExtService;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入id")
	public R<BuildingVO> detail(@RequestParam("id") Long id) {
		Building detail = buildingService.getById(id);
		BuildingVO buildingVO = BuildingWrapper.build().entityVO(detail);
		buildingVO.setBuildingContacts(buildingContactsService.getOne(Wrappers.<BuildingContacts>lambdaQuery().eq(BuildingContacts::getBuildingId,id)));
		BuildingExt buildingExt = buildingExtService.getOne(Wrappers.<BuildingExt>lambdaQuery().eq(BuildingExt :: getBuildingId, id), false);
		buildingVO.setBuildingExt(buildingExt);
		return R.data(buildingVO);
	}

	/**
	 * 查询小区列表
	 *
	 * @param buildingName 小区名称
	 * @return 小区列表
	 */
	@GetMapping("/listBuilding")
	@ApiOperation(value = "查询小区列表")
	public R<List<ListBuildingVO>> listBuilding(String buildingName) {
		return R.data(buildingService.listBuilding(buildingName));
	}

	@GetMapping("/getBuildingType")
	@ApiOperation(value = "获取项目类型集合")
	public R getProjectType() {
		List<DictBiz> list = DictBizCache.getList(PROJECT_TYPE_CODE);
		return R.data(list);
	}

	@GetMapping("/getBrands")
	@ApiOperation(value = "获取品牌下拉数据")
	public R getBrands() {
		return R.data(elevatorBrandService.getBrands());
	}

	/**
	 * 维保小区列表
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/myMaintainBuilding")
	@ApiOperation(value = "维保小区列表")
	public R<IPage<BuildingSelectVO>> myMaintainBuilding(@RequestBody(required = false) BuildingSelectSearchDTO search, @RequestBody Query query) {
		return R.data(buildingService.myMaintainBuilding(search, Condition.getPage(query)));
	}

	/**
	 * 维保小区电梯列表
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/myMaintainBuildingChild")
	@ApiOperation(value = "维保小区电梯列表")
	public R<List<ElevatorSelectVO>> myMaintainBuildingChild(@RequestBody(required = false) BuildingSelectSearchDTO search) {
		return R.data(buildingService.myMaintainBuildingChild(search));
	}

	/**
	 * 维保小区统计
	 *
	 * @return
	 */
	@PostMapping("/myElevatorStatistics")
	@ApiOperation(value = "维保小区统计")
	public R<MyElevatorStatisticsVO> myElevatorStatistics(@RequestBody(required = false) BuildingSelectSearchDTO search) {
		return R.data(buildingService.myElevatorStatistics(search));
	}

	@PostMapping("/comingExpiryProject")
	@ApiOperation(value = "项目合同统计-即将到期项目")
	public R<PieChartVO> comingExpiryProject(@RequestBody(required = false) List<Long> userIds) {
		return R.data(buildingService.comingExpiryProject(userIds));
	}

	@PostMapping("/expiryProject")
	@ApiOperation(value = "项目合同统计-到期未签项目")
	public R<PieChartVO> expiryProject(@RequestBody(required = false) List<Long> userIds) {
		return R.data(buildingService.expiryProject(userIds));
	}

	@PostMapping("/projectElevator")
	@ApiOperation(value = "项目统计-电梯统计")
	public R<PieChartVO> projectElevator(@RequestBody(required = false) List<Long> userIds) {
		return R.data(buildingService.projectElevator(userIds));
	}

	@PostMapping("/projectElevatorUseType")
	@ApiOperation(value = "项目统计-年审梯/非年审梯")
	public R<List<ChartItemVO>> projectElevatorUseType(@RequestBody(required = false) List<Long> userIds) {
		return R.data(buildingService.projectElevatorUseType(userIds));
	}

	@PostMapping("/brandElevator")
	@ApiOperation(value = "项目统计-品牌")
	public R<PieChartVO> brandElevator(@RequestBody(required = false) List<Long> userIds) {
		return R.data(buildingService.brandElevator(userIds));
	}

	/**
	 * 我的电梯-个人管理电梯-小区项目列表
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/personalBuildingList")
	@ApiOperation(value = "我的电梯-个人管理电梯-小区项目列表")
	public R<IPage<PrincipalBuildingListVO>> personalBuildingList(@RequestBody(required = false) BuildingSelectSearchDTO search, @RequestBody Query query) {
		return R.data(buildingService.personalBuildingList(search, Condition.getPage(query)));
	}

	/**
	 * 我的电梯-个人管理电梯-小区项目电梯
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/personalElevatorList")
	@ApiOperation(value = "我的电梯-个人管理电梯-小区项目电梯")
	public R<List<PrincipalElevatorListVO>> personalElevatorList(@RequestBody(required = false) BuildingSelectSearchDTO search) {
		return R.data(buildingService.personalElevatorList(search));
	}

	/**
	 * 我的电梯-部门管理电梯-小区项目列表
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/deptBuildingList")
	@ApiOperation(value = "我的电梯-部门管理电梯-小区项目列表")
	public R<IPage<PrincipalBuildingListVO>> deptBuildingList(@RequestBody(required = false) BuildingSelectSearchDTO search, @RequestBody Query query) {
		return R.data(buildingService.deptBuildingList(search, Condition.getPage(query)));
	}

	/**
	 * 我的电梯-部门管理电梯-小区项目电梯
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/deptElevatorList")
	@ApiOperation(value = "我的电梯-部门管理电梯-小区项目电梯")
	public R<List<PrincipalElevatorListVO>> deptElevatorList(@RequestBody(required = false) BuildingSelectSearchDTO search) {
		return R.data(buildingService.deptElevatorList(search));
	}

	/**
	 * 我的电梯-统计
	 *
	 * @param search
	 * @return
	 */
	@PostMapping("/principalBuildingStatistics")
	@ApiOperation(value = "我的电梯-统计")
	public R<MyElevatorStatisticsVO> principalBuildingStatistics(@RequestBody(required = false) BuildingSelectSearchDTO search) {
		return R.data(buildingService.principalBuildingStatistics(search));
	}

	/**
	 * 报障-项目/电梯-选择
	 * 维保端报障项目，拉取当前维保公司下，所有的项目数据
	 */
	@PostMapping("/myMaintainBuilding/list")
	@ApiOperation(value = "维保端报障项目列表")
	public R<IPage<BuildingSelectVO>> getBuildingPage(@RequestBody Query query, @RequestBody(required = false) BuildingSelectSearchDTO search) {
		IPage<BuildingSelectVO> pageList = buildingService.getBuildingList(Condition.getPage(query), search);
		return R.data(pageList);
	}

	/**
	 * 创建救援工单-小区项目列表选择
	 *
	 * @param search
	 * @return
	 */
	@GetMapping("/rescueBuildingList")
	@ApiOperation(value = "创建救援工单-小区项目列表选择")
	public R<IPage<BuildingSelectVO>> rescueBuildingList(BuildingSelectSearchDTO search, Query query) {
		return R.data(buildingService.rescueBuildingList(search, Condition.getPage(query)));
	}

	/**
	 * 维保小区电梯列表
	 */
	@GetMapping("/rescueElevatorList")
	@ApiOperation(value = "创建救援工单-电梯列表选择")
	public R<IPage<ElevatorSelectVO>> rescueElevatorList(BuildingSelectSearchDTO search, Query query) {
		return R.data(buildingService.rescueElevatorList(search, Condition.getPage(query)));
	}

}
